package Ehcache;


import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import java.util.Map;

/**
 * @author: lqh
 * @description: test
 * @program: Demo
 * @create: 2018-03-23 16:01
 **/
public class helloWorld {
    public static void main(String[] args) {
        //获取Ehcache实例(默认读取classpath目录下的)
        CacheManager cacheManager = CacheManager.getInstance();

        //获取一个Cache
        Cache helloWorldCache = cacheManager.getCache("helloWorldCache");

        //创建一个Element
        Element element1 = new Element("a", "aaaaa");
        Element element2 = new Element("b", "bbbbb");
        Element element3 = new Element("c", "cccccc");
        Element element4 = new Element("d", "ddddddd");
        helloWorldCache.put(element1);
        helloWorldCache.put(element2);
        helloWorldCache.put(element3);
        helloWorldCache.put(element4);

        for (Object key : helloWorldCache.getKeys()) {
            System.out.println("(" + key + "," + helloWorldCache.get(key).getObjectValue() + ")");
        }

        Cache cache = EhcacheManager.getCache("helloWorldCache");
        EhcacheManager.put(cache, "username", "linqihong");
        System.out.println("#######--" + EhcacheManager.getCacheValue(cache, "username", String.class));
    }
}
